﻿using Basic.HrDAL;
using Basic.HrModel.DB;
using Basic.HrModel.Holidays;
using Basic.HrRemoteModel.Holidays.Model;
using SqlSugar;
using WeDonekRpc.Helper;

namespace Basic.HrCollect.Impl
{
    internal class HolidaysCollect : IHolidaysCollect
    {
        private readonly IHolidaysDAL _BasicDAL;

        public HolidaysCollect ( IHolidaysDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public bool Set ( HolidaysAdd data )
        {
            DBHolidays sour = this._BasicDAL.Get<DBHolidays>(a => a.Date == data.Date && a.TimeSpan == data.TimeSpan);
            if ( sour == null )
            {
                this._BasicDAL.Add(data);
                return true;
            }
            else
            {
                return this._BasicDAL.Update(sour, data);
            }
        }
        public void Sync ( int year, HolidaysAdd[] adds )
        {
            DateTime begin = new DateTime(year, 1, 1);
            DateTime end = new DateTime(year, 12, 31);
            var list = this._BasicDAL.Gets(a => SqlFunc.Between(a.Date, begin, end), a => new
            {
                a.Date,
                a.TimeSpan
            });
            adds = adds.FindAll(c => list.IsExists(a => a.Date == c.Date && a.TimeSpan == c.TimeSpan) == false);
            if ( !adds.IsNull() )
            {
                this._BasicDAL.Adds(adds);
            }
        }

        public HolidaysDto[] GetHoliday ( DateTime begin, DateTime end )
        {
            return this._BasicDAL.Gets<HolidaysDto>(a => SqlFunc.Between(a.Date, begin, end));
        }
        public HolidaysDto[] FindHoliday ( DateTime date )
        {
            return this._BasicDAL.Gets<HolidaysDto>(a => a.Date == date);
        }

        public HolidaysInfo[] Gets ( int year, int month )
        {
            DateTime begin = new DateTime(year, month, 1);
            DateTime end = begin.AddMonths(1).AddDays(-1);
            return this._BasicDAL.Gets<HolidaysInfo>(a => SqlFunc.Between(a.Date, begin, end));
        }
    }
}
